package com.minxi.jsp.controller;

import com.minxi.jsp.javabean.UserInfo;
import com.minxi.jsp.service.UserService;
import com.minxi.jsp.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@WebServlet(name = "JspServlet",urlPatterns = "/add")
public class JspServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //1.获取请求数据
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        String userPhone = req.getParameter("userPhone");
        String userSex = req.getParameter("userSex");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //2.请求数据库 JDBC
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            //3.编辑SQL语句 (预编译的执行对象)
            String sql = "insert into user_info(user_name,user_pwd,user_phone,user_sex)values(?,?,?,?);";
            //得到执行对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,passWord);
            preparedStatement.setString(3,userPhone);
            preparedStatement.setInt(4,Integer.parseInt(userSex));

            int rs = preparedStatement.executeUpdate();
            if(rs > 0){
                resp.sendRedirect("jsp/home.jsp");
            }else {
                resp.sendRedirect("jsp/add.jsp");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(null != preparedStatement){
                    preparedStatement.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        //1.获取请求数据
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        String userPhone = req.getParameter("userPhone");
        String userSex = req.getParameter("userSex");

        //2.使用service调用对应方法去处理这个请求
        UserService userService = new UserServiceImpl();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassWord(passWord);
        userInfo.setUserPhone(userPhone);
        userInfo.setUserSex(Integer.parseInt(userSex));
        Boolean add = userService.add(userInfo);
        if(add){
            resp.sendRedirect("jsp/home.jsp");
        }else {
            resp.sendRedirect("jsp/add.jsp");
        }
    }
}
